package com.wemew.securityapi.config.security;

import com.wemew.common.enums.error.Error;
import com.wemew.common.utils.R;
import com.wemew.common.utils.ResponseUtil;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 /** 
   * 作者 CG
   * 时间 2021/1/13 21:08
   * 注释 未授权统一处理异常
   */  
public class UnauthEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        if (e instanceof InsufficientAuthenticationException){
            ResponseUtil.out(httpServletResponse,new R(Error.Error4010));
        }else {
            ResponseUtil.out(httpServletResponse,new R(e));
        }
    }
}
